#include "levelscene.h"

LevelScene::LevelScene(QPixmap mapPixmap, QImage mapMask, QWidget *parent)
    : GameScene((QWidget*)parent)
{
    mainView->setViewport(GLWidget); //use openGL rendering

    // Create the background
    map = new GameBackground(mapPixmap,mainScene);

    // Setup the scene viewport for the size of map we are using
    mainScene->setSceneRect(map->pixmap().rect());
    mainView->centerView();

    // Setup the tools panel
    panelScene = new QGraphicsScene(0,0,this->width(),150,this);
    panelView = new QGraphicsView(panelScene,mainView);

    panelView->resize(this->width(),150);
    panelView->move(0,this->height()-150);
    panelView->setStyleSheet("background: transparent");
    panelView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    panelView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    panelView->setFrameStyle(QFrame::NoFrame);

    panelBackground = new GameBackground(QPixmap(RESOURCE_DIR + "/Kit_MenuBackground_1.png"),panelScene);

    currentTool = 0;

    //Make the minimap
    miniMap = new MiniMap(mapPixmap,panelScene,mainView);
    miniMap->setPos(width()-miniMap->mapRectToScene(miniMap->pixmap().rect()).width() - 10,10);
    miniMap->screenRect->screenPanned(mainView->getCenterPoint());
    QObject::connect(miniMap->screenRect,SIGNAL(screenMove(QPointF)),mainView,SLOT(screenMoved(QPointF)));
    QObject::connect(mainView,SIGNAL(screenPanned(QPointF)),miniMap->screenRect,SLOT(screenPanned(QPointF)));

    //Setup options panel
    optionsPanel = new OptionsPanel(this);
    optionsPanel->move((width()/2)-(optionsPanel->width()/2),((height()-150)/2)-(optionsPanel->height()/2));
    QObject::connect(optionsPanel,SIGNAL(endScene(GameScene::GameScenes)),this,SLOT(endScene(GameScene::GameScenes)));
    QObject::connect(optionsPanel,SIGNAL(resumeScene()),this,SLOT(resumeScene()));

    //----------------------------------------------------------------------
    // Initilize the path finding stuff
    //----------------------------------------------------------------------

    pathFinder = new PathFinder(mapMask,this);
    QObject::connect(pathFinder,SIGNAL(pathSolved(std::vector<void*>,QObject*)),this,SLOT(pathSolved(std::vector<void*>,QObject*)));
}

void LevelScene::start()
{
    mainView->setPanEnabled(true);
    pathFinder->start();
    people.last()->treatPerson();
    grabKeyboard();
    emit startPeople();
}

void LevelScene::endScene(GameScene::GameScenes nextScene)
{
    releaseKeyboard();
    optionsPanel->setEnabled(false);
    GameScene::endScene(nextScene);
    emit stopPeople();
}

void LevelScene::pauseScene()
{
    panelView->setInteractive(false);
    GameScene::pauseScene();
    emit stopPeople();
}

void LevelScene::resumeScene()
{
    panelView->setInteractive(true);
    GameScene::resumeScene();
    emit startPeople();
}

ToolButton * LevelScene::addTool(QString caption, QPixmap toolIcon)
{
    tools.append(new ToolButton(caption,toolIcon,panelScene));
    tools.last()->setPos(10 + ((tools.length()-1)*55),10);

    QObject::connect(tools.last(),SIGNAL(clicked(ToolButton*)),this,SLOT(toolClicked(ToolButton*)));

    return tools.last();
}

Building * LevelScene::addBuilding(QPoint enterence, qreal x, qreal y, QPixmap buildingPixmap)
{
    buildings.append(new Building(enterence,buildingPixmap,mainScene));
    buildings.last()->setPos(x,y);

    QObject::connect(buildings.last(),SIGNAL(clicked(Building*)),this,SLOT(buildingClicked(Building*)));

    return buildings.last();
}

Person * LevelScene::addPerson(qreal x, qreal y, QPixmap personPixmap)
{
    people.append(new Person(pathFinder->getPathMap(), personPixmap, this, mainScene));
    QObject::connect(people.last(),SIGNAL(solvePath(QPoint,QPoint,QObject*)),pathFinder,SLOT(solvePath(QPoint,QPoint,QObject*)));
    QObject::connect(this,SIGNAL(startPeople()),people.last(),SLOT(startPerson()));
    QObject::connect(this,SIGNAL(stopPeople()),people.last(),SLOT(stopPerson()));
    people.last()->setPos(x,y);
    return people.last();
}

void LevelScene::pathSolved(std::vector<void *>path, QObject *receiver)
{
    people.at(people.indexOf((Person*)receiver))->setPath(path);
}

void LevelScene::setBuilding(Person *person, Building *building)
{
    // If the person is currently in a building, remove them
    if (person->currentBuilding != 0)
        person->currentBuilding->removePerson(person);

    // Add them to the new building
    building->addPerson(person);
    person->setCurrentBuilding(building);
}

void LevelScene::toolClicked(ToolButton *sender)
{
    if (sender->getSate())
    {
        if (currentTool)
            currentTool->releaseButton();
        currentTool = sender;
    }
    else
        currentTool = 0;
}

void LevelScene::buildingClicked(Building *sender)
{
    if (currentTool != 0)
    {
        for (int index = 0; index < people.length(); index++)
        {
            if (people.at(index)->getCurrentBuilding() == sender)
                people.at(index)->treatPerson();
        }

        currentTool->releaseButton();
        currentTool = 0;
    }
}

void LevelScene::keyPressEvent(QKeyEvent *event)
{
    if (event->key() == Qt::Key_Escape)
    {
        pauseScene();
        optionsPanel->setEnabled(true);
    }
}
